from client.pages.InfoWin import *
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtCore import pyqtSignal, Qt

import json
import webbrowser

class InfoPage(QMainWindow, Ui_InfoWin):
    sound_signal = pyqtSignal(int)

    def __init__(self):
        super(InfoPage, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.sound = 50
        self.load_config()

        self.close_button.clicked.connect(self.OnClose)
        self.sound_slider.sliderReleased.connect(self.OnChange)
        self.textBrowser.anchorClicked.connect(self.openAlink)

    def openAlink(self, a0):
        url = a0.toString()
        webbrowser.open(url)

    def load_config(self):
        config_path = "client/.cache/config.json"
        try:
            with open(config_path, "r") as f:
                self.sound = json.load(f)["sound"]
        except Exception as e:
            print("info page load config:", e)
            self.sound = 50
        self.sound_slider.setValue(self.sound)

    def OnChange(self):
        self.sound = self.sound_slider.value()
        self.sound_signal.emit(int(self.sound))

    def OnClose(self):
        self.close()